package com.mytrip.model;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mytrip.base.dao.impl.IBatisDaoUtils;
import com.mytrip.base.model.BaseModel;

/**
 * Base class for Model objects.
 */
public abstract class BaseModelAdapter implements BaseModel{

	protected final Log log = LogFactory.getLog(getClass());

	public static final int FLAG_NO = 0;

	public static final int FLAG_YES = 1;

	public static final int IS_DELETE_NO = 0;

	public static final int IS_DELETE_YES = 1;

	public String toString(){
		StringBuffer propBuffer = new StringBuffer();
		Field[] fields = this.getClass().getDeclaredFields();
		propBuffer.append("[").append(this.getClass()).append("]");

		for (Field field : fields) {
			String fieldName = field.getName();
			Object fieldValue = null;
			String getterMethod = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
			try {
				Method getMethod = this.getClass().getMethod(getterMethod, (Class[]) null);
				Object o = getMethod.invoke(this, (Object[]) null);
				if (o instanceof Map || o instanceof List) {
					continue;
				}

				if (o instanceof BaseModel) {
					fieldName += "Id";
					fieldValue = IBatisDaoUtils.getPrimaryKeyValue((BaseModel) o);
				} else {
					fieldValue = o;
				}

				propBuffer.append(fieldName).append(":").append(fieldValue).append(",");

			} catch (Exception e) {
			}

		}

		return propBuffer.substring(0, propBuffer.length() - 1);
	}

}
